#/bin/bash
#ORI_DIR=$(dirname $0)
ORI_DIR=$PWD
BASE_DIR=""


function red {
    echo -e "\033[0;31m$1\033[0m"
}

function  green {
    echo -e "\033[0;32m$1\033[0m"
}

function success {
    green $1
}

function fail {
    red $1
}

realpath() {
    [[ $1 = /* ]] && BASE_DIR=$(dirname $1) || BASE_DIR=$(dirname "$PWD/${1#./}")
    cd $BASE_DIR
    BASE_DIR=`pwd`
    #echo $(dirname "$PWD/${1#./}")
    #cd $(dirname "$PWD/${1#./}")
}

config_vim() {
    echo "===step $1: config vim"
    #TODO check for existance of non symbolic dir and filename. be cursously
    VIM_DIR=$BASE_DIR"/vim"
    echo "vim config file location is $VIM_DIR"

    if [ -d $HOME'/.vim' ]; then 
        if [ -L $HOME'/.vim' ]; then
            echo ".vim symbolic link dir exiests. unlinking ..."
            unlink $HOME'/.vim'
        else 
            echo "dir $HOME'/.vim' exists"
            echo "do rm -rf of $HOME'.vim' ??"
        fi
    fi
    ln -s $VIM_DIR $HOME"/.vim"
    if [ 0 -eq $? ]; then
        green 'linking .vim successed'
    else 
        red 'linking .vim failed'
    fi

    [ -d $VIM_DIR"/bundle/Vundle.vim" ] || (mkdir -p $VIM_DIR"/bundle"; echo "not has"; cd $VIM_DIR"/bundle"; git clone https://github.com/VundleVim/Vundle.vim.git)
    if [ -f $HOME'/.vimrc' ]; then
        if [ -L $HOME'/.vimrc' ]; then
            echo '.vimrc symbolic link exists unlinking ...'
            unlink $HOME'/.vimrc'
        else 
            echo 'plain $HOME'/.vimrc' file exists'
            echo "do rm $HOME'/.vimrc' ?"
        fi
    fi
    echo "linking .vimrc"
    ln -s $VIM_DIR'/vimrc' $HOME'/.vimrc'
    if [ 0 -eq $? ]; then
        green 'linking .vimrc successed'
    else
        red 'linking .vimrc failed'
    fi
}

config_git(){
    echo "===step $1: config git"
    CONFIG_DIR=$BASE_DIR"/configs"
    if [ -f $HOME'/.gitconfig' ]; then
        if [ -L $HOME'/.gitconfig' ]; then
            echo '.aliases symbolic link exists unlinking ...'
            unlink $HOME'/.gitconfig'
        else 
            echo 'plain $HOME'/.gitconfig' file exists'
            echo "do rm $HOME'/.gitconfig' ?"
        fi
    fi
    echo "linking .gitconfig"
    ln -s $CONFIG_DIR'/gitconfig' $HOME'/.gitconfig'
    if [ 0 -eq $? ]; then
        green 'linking .gitconfig successed'
    else
        red 'linking .gitconfig failed'
    fi
}


config_aliases(){
    echo "===step $1: config aliases"
    CONFIG_DIR=$BASE_DIR"/configs"
    if [ -f $HOME'/.aliases' ]; then
        if [ -L $HOME'/.aliases' ]; then
            echo '~/.aliases symbolic link exists unlinking ...'
            unlink $HOME'/.aliases'
        else 
            echo 'plain $HOME'/.aliases' file exists'
            echo "do rm $HOME'/.aliases' ?"
        fi
    fi
    echo "linking .aliases"
    ln -s $CONFIG_DIR'/aliases' $HOME'/.aliases'
    if [ 0 -eq $? ]; then
        green 'linking .aliases successed'
    else 
        red 'linking .aliases red'
    fi

    if [ -f $HOME'/.bashrc' ]; then
        xx=`grep '\[ -f ~/\.aliases \] && . ~/.aliases' $HOME'/.bashrc' | wc -l`
        if [ $xx == 0 ]; then
            #red "appending aliases to .bashrc"
            echo '[ -f ~/.aliases ] && . ~/.aliases' >> $HOME'/.bashrc'
        else
            #red "grep res is $xx"
            red "aliases already sourceed"
        fi
    fi
}

config_functions(){
    echo "===step $1: config functions"
    CONFIG_DIR=$BASE_DIR"/configs"
    if [ -f $HOME'/.functions' ]; then
        if [ -L $HOME'/.functions' ]; then
            echo '~/.functions symbolic link exists unlinking ...'
            unlink $HOME'/.functions'
        else 
            echo 'plain $HOME'/.functions' file exists'
            echo "do rm $HOME'/.functions' ?"
        fi
    fi
    echo "linking .functions"
    ln -s $CONFIG_DIR'/functions' $HOME'/.functions'
    if [ 0 -eq $? ]; then
        green 'linking .functions successed'
    else 
        red 'linking .functions red'
    fi

    if [ -f $HOME'/.bashrc' ]; then
        xx=`grep '\[ -f ~/\.functions \] && . ~/.functions' $HOME'/.bashrc' | wc -l`
        if [ $xx == 0 ]; then
            #red "appending functions to .bashrc"
            echo '[ -f ~/.functions ] && . ~/.functions' >> $HOME'/.bashrc'
        else
            #red "grep res is $xx"
            red "functions already sourceed"
        fi
    fi

    if [ 0 -eq $? ]; then
        green 'appending source .functions to .bashrc successed'
    else
        red 'appending source .functions to .bashrc failed'
    fi
    
}

config_scripts(){
    echo "===step $1: config scipts"
    DDIR=$BASE_DIR
    if [ -d $HOME'/.scripts' ]; then
        if [ -L $HOME'/.scripts' ]; then
            echo '~/.scripts symbolic link exists unlinking ...'
            unlink $HOME'/.scripts'
        else 
            echo 'plain $HOME'/.scripts' file exists'
            echo "do rm $HOME'/.scripts' ?"
        fi
    fi
    echo "linking .scripts"
    ln -s $DDIR'/scripts' $HOME'/.scripts'
    if [ 0 -eq $? ]; then
        green 'linking .scripts successed'
    else
        red 'linking .scripts failed'
    fi
}

config_tmux(){
    echo "===step $1: config tmux"
    CONFIG_DIR=$BASE_DIR"/configs"
    TMUX_DIR=$BASE_DIR"/tmux"
    echo "tmux config file location is $TMUX_DIR"

    if [ -d $HOME'/.tmux' ]; then 
        if [ -L $HOME'/.tmux' ]; then
            echo ".vim symbolic link dir exiests. unlinking ..."
            unlink $HOME'/.tmux'
        else 
            echo "dir $HOME'/.tmux' exists"
            echo "do rm -rf of $HOME'.tmux' ??"
        fi
    fi
    ln -s $TMUX_DIR $HOME"/.tmux"
    if [ 0 -eq $? ]; then
        green 'linking .tmux successed'
    else 
        red 'linking .tmux failed'
    fi
    echo $TMUX_DIR"/plugins/tpm"
    [ -d $TMUX_DIR"/plugins/tpm" ] || (mkdir -p $VIM_DIR"/bundle"; echo "not has"; cd $TMUX_DIR"/plugins"; git clone https://github.com/tmux-plugins/tpm)
    if [ -f $HOME'/.tmux.conf' ]; then
        if [ -L $HOME'/.tmux.conf' ]; then
            echo '~/.tmux.conf symbolic link exists unlinking ...'
            unlink $HOME'/.tmux.conf'
        else 
            echo 'plain $HOME'/.tmux.conf' file exists'
            echo "do rm $HOME'/.tmux.conf' ?"
        fi
    fi
    echo "linking .tmux.conf"
    ln -s $TMUX_DIR'/tmux.conf' $HOME'/.tmux.conf'
    if [ 0 -eq $? ]; then
        green 'linking .tmux.conf successed'
    else
        red 'linking .tmux.conf failed'
    fi
}

config_todo(){
    echo "===step $1: config todo"
    #TODO check for existance of non symbolic dir and filename. be cursously
    DDIR=$BASE_DIR"/todo"

    if [ -d $HOME'/.todo' ]; then 
        if [ -L $HOME'/.todo' ]; then
            echo ".todo symbolic link dir exiests. unlinking ..."
            unlink $HOME'/.todo'
        else 
            echo "dir $HOME'/.todo' exists"
            echo "do rm -rf of $HOME'.todo' ??"
        fi
    fi
    ln -s $DDIR $HOME"/.todo"
    if [ 0 -eq $? ]; then
        green 'linking .todo successed'
    else
        red 'linking .todo failed'
    fi
    if [ -f $HOME'/.todo.txt' ]; then
        if [ -L $HOME'/.todo.txt' ]; then
            echo '~/.todo.txt symbolic link exists unlinking ...'
            unlink $HOME'/.todo.txt'
        else 
            echo 'plain $HOME'/.todo.txt' file exists'
            echo "do rm $HOME'/.todo.txt' ?"
        fi
    fi
    echo "linking .todo.txt"
    ln -s $DDIR'/todo.txt' $HOME'/.todo.txt'
    if [ 0 -eq $? ]; then
        green 'linking .todo.txt successed'
    else 
        green 'linking .todo.txt failed'
    fi
}


echo "original dir $ORI_DIR"
realpath "$0"
echo "base dir is $BASE_DIR"

config_vim "1"

config_git "2"

config_tmux "3"

config_aliases "4"

config_functions "5"

config_scripts "6"

config_todo "7"
